#include <stdio.h>
#include "Utils.h"

std::string Utils::SafeString(const std::string &str)
{
	std::string ret;
	for (size_t i = 0; i < str.size(); i++)
	{
		switch (str[i])
		{
		case '\'':
		case '\\':
		case 34:
			ret += '\\';
			break;
		default:
			ret += str[i];
		}
	}
	return ret;
}

std::string Utils::UnSafeString(const std::string &str)
{
	std::string ret;
	for (size_t i = 0; i < str.size(); i++)
	{
		if (str[i] == '\\')
		{
			i++;
		}
		if (i < str.size())
		{
			ret += str[i];
		}
	}
	return ret;
}

std::string Utils::XmlSafeString(const std::string &str)
{
	std::string ret;
	for (size_t i = 0; i < str.size(); i++)
	{
		switch (str[i])
		{
		case '&':ret += "&amp;";break;
		case '<':ret += "&lt;";break;
		case '>':ret += "&gt;";break;
		case '"':ret += "&quot;";break;
		case '\'':ret += "&apos;";break;
		default:ret += str[i];
		}
	}
	return ret;
}

int64_t Utils::ToBigInt(const std::string &str)
{
	int64_t ret = 0;
	bool sign = false;
	size_t i = 0;
	if (str[i] == '-')
	{
		sign = true;
		i++;
	}

	for (; i < str.size(); i++)
	{
		ret = ret * 10 + (str[i] - 48);
	}
	return sign ? -ret : ret;
}

uint64_t Utils::ToUBigInt(const std::string &str)
{
	uint64_t ret = 0;
	for (size_t i = 0; i < str.size(); i++)
	{
		ret = ret * 10 + (str[i] - 48);
	}
	return ret;
}